package com.cx.web.controller;

import com.alibaba.fastjson.JSON;
import com.cx.common.util.IpUtils;
import com.cx.common.util.StringUtils;
import com.cx.meta.entity.UserClient;
import com.cx.meta.entity.UserLogin;
import com.cx.meta.entity.UserRegister;
import com.cx.meta.enums.CertificateType;
import com.cx.meta.exception.CxErrorCode;
import com.cx.meta.exception.CxException;
import com.cx.meta.model.po.UserQueryParams;
import com.cx.meta.model.vo.PageData;
import com.cx.meta.model.vo.UserInfo;
import com.cx.meta.model.vo.UserToken;
import com.cx.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author flysand
 * @package com.cx.web.controller
 * @date 2021-11-07 18:09
 * @des UserController : 用户Controller
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);


    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegister 注册信息
     */
    @PostMapping("/register")
    public void registerUser(@RequestBody UserRegister userRegister) {

        //参数校验
        if (verifyRegister(userRegister)) {
            // 注册用户


        }


    }


    /**
     * 基础参数校验 非空 格式
     *
     * @param userRegister
     * @return 校验结果
     */
    private boolean verifyRegister(UserRegister userRegister) {
        if (userRegister == null) {
            throw new CxException(CxErrorCode.REGISTER_INFO_EMPTY);
        }
        CertificateType certificateType = userRegister.getRegisterType();
        String mobile = userRegister.getMobileNumber();
        String email = userRegister.getEmail();
        switch (certificateType) {
            case MOBILE:
                if (StringUtils.isBlank(mobile)) {
                    // 手机号不能为空
                    throw new CxException(CxErrorCode.MOBILE_EMPTY);
                }
                break;
            case EMAIL:
                if (StringUtils.isBlank(email)) {
                    // 邮箱不能为空
                    throw new CxException(CxErrorCode.EMAIL_EMPTY);
                }
                break;
            default:
        }

        String password = userRegister.getPassword();
        if (StringUtils.isBlank(password)) {
            throw new CxException(CxErrorCode.PASSWORD_EMPTY);
        }

        return true;
    }

    /**
     * 用户登录
     *
     * @param userLogin 登录信息
     * @param request   请求request
     * @return 认证token
     */
    @PostMapping("/userLogin")
    public String login(@RequestBody UserLogin userLogin, HttpServletRequest request) {

        long start = System.currentTimeMillis();
        String ip = IpUtils.getIp(request);
        logger.debug("用户登录:" + JSON.toJSONString(userLogin) + ",IP:" + ip);
        String browser = IpUtils.getBrowser(request);
        // 后续增加 mac 和 browser
        UserClient userClient = new UserClient(ip, null, browser);
        String token = "";
        // 用户登录
        if (userLogin.getCertificateType() == CertificateType.MOBILE) {
            // 校验用户
            String mobile = userLogin.getMobileNumber();
            String code = userLogin.getCode();
            if (StringUtils.isBlank(mobile) || StringUtils.isBlank(code)) {
                throw new CxException(CxErrorCode.MOBILE_OR_CODE_EMPTY);
            }
            token = userService.mobileLogin(mobile, code, userClient);
        } else {
            // 普通登录
            String loginName = userLogin.getLoginName();
            String password = userLogin.getPassword();
            if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)) {
                throw new CxException(CxErrorCode.LOGIN_NAME_OR_PASSWORD_EMPTY);
            }
            token = userService.userLogin(loginName, password, userClient);
        }
        long end = System.currentTimeMillis();
        logger.debug("登录用时(ms)：" + (end - start));
        return token;
    }

    @RequestMapping(value = "/checkToken", method = {RequestMethod.GET, RequestMethod.POST})
    public UserToken checkUserToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        return userService.getUserToken(token);
    }

    @PostMapping("/getUserByQueryParams")
    public PageData<UserInfo> getUserByQueryParams(@RequestBody UserQueryParams userQueryParams) {
        logger.debug("get all users:" + JSON.toJSONString(userQueryParams));
        return userService.getUserByQueryParams(userQueryParams);
    }

    @PostMapping("/modifySignature")
    public String modifySignature(@RequestParam String signature, HttpServletRequest request) {
        if (StringUtils.isBlank(signature)) {
            return "";
        }
        // 修改
        return userService.modifySignature(request.getHeader("token"), signature);
    }


}
